#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class VoteRecordDetailProvider : CY.GFive.Core.Providers.DALProvider.IVoteRecordDetailProvider
	{
		#region Sql
		private static readonly string SqlSelectVoteRecordDetail = "SELECT [Id], [VoteRecordID],[VoteItemID],[VoteAnswerID] FROM [VoteRecordDetail] WHERE  [Id]=@Id";

		private static readonly string SqlInsertVoteRecordDetail = "INSERT INTO [VoteRecordDetail]([VoteRecordID],[VoteItemID],[VoteAnswerID]) values(@VoteRecordID,@VoteItemID,@VoteAnswerID);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateVoteRecordDetail = "UPDATE [VoteRecordDetail] SET [VoteRecordID] = @VoteRecordID,[VoteItemID] = @VoteItemID,[VoteAnswerID] = @VoteAnswerID WHERE  [Id]=@Id";

		private static readonly string SqlDeleteVoteRecordDetail = "DELETE [VoteRecordDetail] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllVoteRecordDetail = "SELECT [Id], [VoteRecordID],[VoteItemID],[VoteAnswerID] FROM [VoteRecordDetail]";
		#endregion

		#region IVoteRecordDetailProvider Members
		public Core.Business.VoteRecordDetail Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectVoteRecordDetail);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.VoteRecordDetail voteRecordDetail = new Core.Business.VoteRecordDetail();

				if (!reader.IsDBNull(0)) voteRecordDetail.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) voteRecordDetail.VoteRecordID = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) voteRecordDetail.VoteItemID = reader.GetInt32(2);
				if (!reader.IsDBNull(3)) voteRecordDetail.VoteAnswerID = reader.GetInt32(3);

				return voteRecordDetail;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.VoteRecordDetail voteRecordDetail)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteRecordID", SqlDbType.Int, voteRecordDetail.VoteRecordID);
			sql.AddParameter("@VoteItemID", SqlDbType.Int, voteRecordDetail.VoteItemID);
			sql.AddParameter("@VoteAnswerID", SqlDbType.Int, voteRecordDetail.VoteAnswerID);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertVoteRecordDetail);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) voteRecordDetail.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.VoteRecordDetail voteRecordDetail)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteRecordID", SqlDbType.Int, voteRecordDetail.VoteRecordID);
			sql.AddParameter("@VoteItemID", SqlDbType.Int, voteRecordDetail.VoteItemID);
			sql.AddParameter("@VoteAnswerID", SqlDbType.Int, voteRecordDetail.VoteAnswerID);
			sql.AddParameter("@Id", SqlDbType.Int, voteRecordDetail.Id);
			sql.ExecuteSql(SqlUpdateVoteRecordDetail);
		}

		public void Delete(Core.Business.VoteRecordDetail voteRecordDetail)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, voteRecordDetail.Id);
			sql.ExecuteSql(SqlDeleteVoteRecordDetail);
		}

		public IList<Core.Business.VoteRecordDetail> GetAllVoteRecordDetail()
		{
			IList<Core.Business.VoteRecordDetail> voteRecordDetaillist = new List<Core.Business.VoteRecordDetail>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllVoteRecordDetail);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.VoteRecordDetail voteRecordDetail = new Core.Business.VoteRecordDetail();

					if (!reader.IsDBNull(0)) voteRecordDetail.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) voteRecordDetail.VoteRecordID = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) voteRecordDetail.VoteItemID = reader.GetInt32(2);
					if (!reader.IsDBNull(3)) voteRecordDetail.VoteAnswerID = reader.GetInt32(3);

					voteRecordDetail.MarkOld();
					voteRecordDetaillist.Add(voteRecordDetail);
				}
				reader.Close();
			}
			return voteRecordDetaillist;
		}
		#endregion
	}
}
